<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
    margin: 0;
    padding: 0;
}
.wrap{
    width: 500px;
    padding: 20px;
    background-color: #eee;
    margin: 30px auto 0;
}
.wrap h1{
    font-size: 20px;
    color: lightcoral;
    margin-bottom: 5px;
    font-weight: normal;
}
.wrap .impression{
    width: 480px;
    font-size: 18px;
    padding: 5px 8px;
}
.box{
    overflow: hidden;
    margin: 10px 0;
}
.box .btn{
    float: right;
    font-size: 16px;
    padding: 2px 8px;
}
.wrap h3{
    font-size: 20px;
    color: lightseagreen;
    font-weight: normal;
}
.show{
    height: 300px;
    background-color: #fff;
    position: relative;
}
.show p{
    padding: 5px 18px 5px 5px;
    background-color: lightgreen;
    position: absolute;
    left: 45px;
    top: 123px;
    color: #fff;
}
.show p span{
    position: absolute;
    right: 4px;
    top: 0px;
    color: red;
    cursor: pointer;
}
</style>
</head>
<body>
    
<div class="wrap">
    <h1>好友印象：</h1>
    <input type="text" class="impression" placeholder="请输入好友印象">
    <div class="box">
        <button class="btn">提交</button>
    </div>
    <h3>严海静的好友印象：</h3>
    <div class="show">
        <!-- <p>严海静真英俊<span>X</span></p> -->
    </div>
</div>

<script src="./utils.js"></script>
<script>
// 获取元素
var impression = $('.impression');
var btn = $('.btn');
var show = $('.show');

// 给 提交 添加点击事件
btn.onclick = function (){
    // 获取输入框的值
    var val = impression.value;
    // 空值判断
    if (!val) {
        alert('亲，输入内容不能为空！');
        return;// 结束函数
    }
    // 长度限制6个字符
    if (val.length > 6) {
        val = val.substr(0,6);
    }
    // 创建p标签
    var p = document.createElement('p');
    // 给p标题添加内容
    p.innerHTML = val+'<span>X</span>';
    // 给p添加随机的背景颜色
    p.style.backgroundColor = randomColor();
    // 设置p的随机位置
    p.style.left = randomInt(0,show.clientWidth-119) + 'px';
    p.style.top = randomInt(0,show.clientHeight-31) + 'px';
    // 向show中添加p
    show.appendChild(p);

    // 清空输入框
    impression.value = '';

    // 给p中的span添加点击事件
    p.lastElementChild.onclick = function (){
        // 删除span所在的p标签
        p.remove();
    }
}

</script>
</body>
</html>